import 'dart:math';

import 'package:flutter/material.dart';
import 'package:tiktokadult/hj_model/acg/CartoonMoreList.dart';
import 'package:tiktokadult/hj_utils/date_time_util.dart';

import '../actress_info.dart';
import '../cartoon_media_info.dart';
import '../splash/ads_model.dart';
import '../video_model.dart';
import 'all_module_info.dart';

class ModuleDetailModel {
  List<AllSection>? allSection; //所有专题
  List<VideoModel>? allVideoInfo; //所有视频
  List<AllModule>? allModule; //所有模块
  List<ActressInfo>? allActress;
  List<CartoonMediaInfo>? allMediaInfo;

  PreferencesModel? preferencesModel; //猜你喜欢
  bool? hasNext;

  List<VideoModel>? communityHotVideo;

  ModuleDetailModel({
    this.allActress,
    this.allModule,
    this.allSection,
    this.allVideoInfo,
    this.hasNext,
    this.communityHotVideo,
  });

  ModuleDetailModel.fromJson(Map<String, dynamic>? json) {
    json ??= {};
    if (json['allSection'] is List) {
      allSection = [];
      json['allSection'].forEach((v) {
        allSection?.add(AllSection.fromJson(v));
      });
    }
    if (json['allVideoInfo'] is List) {
      allVideoInfo = [];
      json['allVideoInfo'].forEach((v) {
        allVideoInfo?.add(VideoModel.fromJson(v));
      });
    }
    if (json['allModule'] is List) {
      allModule = [];
      json['allModule'].forEach((v) {
        allModule?.add(AllModule.fromJson(v));
      });
    }
    communityHotVideo = (json['communityHotVideo'] as List?)?.map((e) => VideoModel.fromJson(e)).toList();
    if (json['allActress'] is List) {
      allActress = [];
      json['allActress'].forEach((v) {
        allActress?.add(ActressInfo.fromJson(v));
      });
    }
    if (json['allMediaInfo'] is List) {
      allMediaInfo = [];
      json['allMediaInfo'].forEach((v) {
        allMediaInfo?.add(CartoonMediaInfo.fromJson(v));
      });
    }
    if (json['guessYouLikeMedia'] != null) {
      preferencesModel = PreferencesModel.fromJson(json['guessYouLikeMedia']);
    }

    hasNext = json['hasNext'];
  }
}

class AllSection {
  String? sectionID;
  String? sectionName;
  String? sectionTitle;
  List<TagsBean>? allTags;
  List<VideoModel>? allVideoInfo;
  List<CartoonMediaInfo>? allMediaInfo;
  List<ActressInfo>? allActress;
  String? sectionDesc;

  int? linkType;
  String? linkUrl;
  String? desc;

  /*
  showType:
  OneLargeAndFourSmall = 0  // 0  一大四小
HScroll15            = 1  // 1  1.5左右滑动(横)
HScroll25            = 2  // 2  2.5左右滑动(横)
FourGrid             = 3  // 3  四宫格(横acg+影视共用)
SixGrid              = 4  // 4  六宫格(横)
HListGrid            = 5  // 5  小列表 (横)
HListBigGrid         = 6  // 6  大列表 (横)
VerticalScroll25     = 7  // 7  2.5左右滑动(竖ACG)
VerticalFourGrid     = 8  // 8  四宫格(竖ACG)
VerticalSixGrid      = 9  // 9  六宫格(竖ACG)
VerticalNineGrid     = 10 // 10 九宫格(竖ACG)
VerticalListGrid     = 11 // 11 小列表 (竖ACG)
GuessYouLike         = 12 // 12 猜你喜欢
  */

  int? showType;
  bool get isGuessLike => showType == 12; // 猜你喜欢
  int? type;
  int? voteCount;
  int? disVoteCount;
  int? collCount;
  int? viewCounts;
  String? sectionCover;
  bool? collected;
  int? workCount;
  ///videoModel是否随机广告
  AdsInfoModel? randomAdsInfo;
  List<AdsInfoModel>? adsInfoArr;
  bool? hasNext;

  bool isRandomAd() {
    return randomAdsInfo != null;
  }

  bool isAdsArr() {
    return adsInfoArr != null;
  }

  AllSection(
      {this.allActress,
      this.allVideoInfo,
      this.allMediaInfo,
      this.allTags,
      this.linkType,
      this.linkUrl,
      this.sectionID,
      this.sectionName,
      this.showType,
      this.voteCount, // 大胸投票
      this.disVoteCount, //贫胸投票
      this.type,
      this.desc,
      this.sectionDesc});

  AllSection.fromJson(dynamic json) {
    if (json['allActress'] != null) {
      allActress = [];
      json['allActress'].forEach((v) {
        allActress?.add(ActressInfo.fromJson(v));
      });
    }
    workCount = json['workCount'];
    desc = json['desc'];
    if (json['allVideoInfo'] != null) {
      allVideoInfo = [];
      json['allVideoInfo'].forEach((v) {
        allVideoInfo?.add(VideoModel.fromJson(v));
      });
    }
    if (json['allMediaInfo'] != null) {
      allMediaInfo = [];
      json['allMediaInfo'].forEach((v) {
        allMediaInfo?.add(CartoonMediaInfo.fromJson(v));
      });
    }
    if (json['allTags'] != null) {
      allTags = [];
      json['allTags'].forEach((v) {
        allTags?.add(TagsBean.fromMap(v));
      });
    }
    sectionDesc = json['sectionDesc'];
    collCount = json['collCount'];
    viewCounts = json['viewCounts'];
    linkType = json['linkType'];
    linkUrl = json['linkUrl'];
    sectionID = json['sectionID'] ?? json['id'];
    sectionName = json['sectionName'];
    showType = json['showType'];
    type = json['type'];
    voteCount = json['voteCount'];
    disVoteCount = json['disVoteCount'];
    collected = json['collected'];
    sectionCover = json['sectionCover'];
    sectionTitle = json['sectionTitle'];
    hasNext = json['hasNext'];
  }
}

class DailyRecommend {
  String? background;
  String? desc;
  String? endTime;
  String? id;
  bool? isCollect;
  String? mediaId;
  String? peopleImg;
  String? recommendTag;
  String? startTime;
  int? style; //随机色
  List<Tags>? tags;
  String? title;
  String? titleImg;
  int? type;
  String? horizontalCover;
  String? verticalCover;
  String? mediaTitle;
  String? mediaType;

  DailyRecommend({
    this.background,
    this.desc,
    this.endTime,
    this.id,
    this.isCollect,
    this.mediaId,
    this.peopleImg,
    this.recommendTag,
    this.startTime,
    this.style,
    this.tags,
    this.title,
    this.titleImg,
    this.type,
    this.horizontalCover,
    this.verticalCover,
    this.mediaTitle,
    this.mediaType,
  });

  String? get tagsDesc {
    return tags?.map((e) => e.name).join('·') ?? '';
  }

  String? get coverH {
    return (horizontalCover?.isNotEmpty == true) ? horizontalCover : verticalCover;
  }

  String? get coverV {
    return (verticalCover?.isNotEmpty == true) ? verticalCover : horizontalCover;
  }

  String? get countDown {
    return DateTimeUtil.calTimediffFromNow(endTime);
  }

  DailyRecommend.fromJson(Map<String, dynamic> json) {
    background = json['background'];
    desc = json['desc'];
    endTime = json['endTime'];
    id = json['id'];
    isCollect = json['isCollect'];
    mediaId = json['mediaId'];
    peopleImg = json['peopleImg'];
    recommendTag = json['recommendTag'];
    startTime = json['startTime'];
    style = json['style'];
    if (style == null || style == 0) {
      style = Random().nextInt(5) + 1;
    }
    if (json['tags'] != null) {
      tags = <Tags>[];
      json['tags'].forEach((v) {
        tags!.add(new Tags.fromJson(v));
      });
    }
    title = json['title'];
    titleImg = json['titleImg'];
    type = json['type'];
    horizontalCover = json['horizontalCover'];
    verticalCover = json['verticalCover'];
    mediaTitle = json['mediaTitle'];
    mediaType = json['mediaType'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['background'] = this.background;
    data['desc'] = this.desc;
    data['endTime'] = this.endTime;
    data['id'] = this.id;
    data['isCollect'] = this.isCollect;
    data['mediaId'] = this.mediaId;
    data['peopleImg'] = this.peopleImg;
    data['recommendTag'] = this.recommendTag;
    data['startTime'] = this.startTime;
    data['style'] = this.style;
    if (this.tags != null) {
      data['tags'] = this.tags!.map((v) => v.toJson()).toList();
    }
    data['title'] = this.title;
    data['titleImg'] = this.titleImg;
    data['type'] = this.type;
    data['mediaType'] = this.mediaType;
    return data;
  }
}

class Tags {
  String? coverImg;
  String? description;
  bool? hasCollected;
  String? id;
  String? name;
  int? playCount;

  Tags({this.coverImg, this.description, this.hasCollected, this.id, this.name, this.playCount});

  Tags.fromJson(Map<String, dynamic> json) {
    coverImg = json['coverImg'];
    description = json['description'];
    hasCollected = json['hasCollected'];
    id = json['id'];
    name = json['name'];
    playCount = json['playCount'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['coverImg'] = this.coverImg;
    data['description'] = this.description;
    data['hasCollected'] = this.hasCollected;
    data['id'] = this.id;
    data['name'] = this.name;
    data['playCount'] = this.playCount;
    data.removeWhere((key, value) => value == null);
    return data;
  }
}

class TaskList {
  List<AwardList>? awardList;
  String? endTime;
  String? id;
  String? objId;
  int? progress;
  String? recommendId;
  String? startTime;
  int? state; //0-未完成 1-已完成待领取 2-已领取
  int? target;
  int? targetEnum;
  String? title;
  int? type;
  String? icon;

  String get stateDesc {
    if (state == 0) {
      return '去完成';
    } else if (state == 1) {
      return '待领取';
    } else if (state == 2) {
      return '已完成';
    }
    return '';
  }

  String get taskPoint {
    if (awardList?.isNotEmpty == true && (awardList?.length ?? 0) >= 1) {
      AwardList? award = awardList?.first;
      return '+${award?.num ?? 0}积分';
    }
    return '+0积分';
  }

  //0-未完成 1-已完成待领取 2-已领取
  Color get stateColor {
    if (state == 0) {
      return Color(0xffFF004F);
    } else if (state == 1) {
      return Color(0xffFFD5A8);
    } else if (state == 2) {
      return Colors.white.withOpacity(0.2);
    }
    return Color(0xffFFD5A8);
  }

  //0-未完成 1-已完成待领取 2-已领取
  Color get btnTextColor {
    if (state == 0) {
      return Colors.white;
    } else if (state == 1) {
      return Color(0xff692A1F);
    } else if (state == 2) {
      return Colors.white;
    }
    return Colors.white;
  }

  String get progrssDesc {
    return '($progress/$target)';
  }

  TaskList(
      {this.awardList,
      this.endTime,
      this.id,
      this.objId,
      this.progress,
      this.recommendId,
      this.startTime,
      this.state,
      this.target,
      this.targetEnum,
      this.title,
      this.type,
      this.icon});

  TaskList.fromJson(Map<String, dynamic> json) {
    if (json['awardList'] != null) {
      awardList = <AwardList>[];
      json['awardList'].forEach((v) {
        awardList!.add(new AwardList.fromJson(v));
      });
    }
    endTime = json['endTime'];
    id = json['id'];
    objId = json['objId'];
    progress = json['progress'];
    recommendId = json['recommendId'];
    startTime = json['startTime'];
    state = json['state'];
    target = json['target'];
    targetEnum = json['targetEnum'];
    title = json['title'];
    type = json['type'];
    icon = json['icon'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.awardList != null) {
      data['awardList'] = this.awardList!.map((v) => v.toJson()).toList();
    }
    data['endTime'] = this.endTime;
    data['id'] = this.id;
    data['objId'] = this.objId;
    data['progress'] = this.progress;
    data['recommendId'] = this.recommendId;
    data['startTime'] = this.startTime;
    data['state'] = this.state;
    data['target'] = this.target;
    data['targetEnum'] = this.targetEnum;
    data['title'] = this.title;
    data['type'] = this.type;
    return data;
  }
}

/// 任务专区的视频
class WareDiscountAreaData {
  String? id;
  String? title;
  String? desc;
  int? discount;
  int? status;
  int? sortCode;
  int? recommendNum;
  String? updatedAt;
  String? createdAt;

  WareDiscountAreaData({
    this.id,
    this.title,
    this.desc,
    this.discount,
    this.status,
    this.sortCode,
    this.recommendNum,
    this.updatedAt,
    this.createdAt,
  });

  WareDiscountAreaData.fromJson(Map<String, dynamic> json) {
    id = json["id"];
    title = json["title"];
    desc = json["desc"];
    discount = json["discount"];
    status = json["status"];
    sortCode = json["sortCode"];
    recommendNum = json["recommendNum"];
    updatedAt = json["updatedAt"];
    createdAt = json["createdAt"];
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "desc": desc,
        "discount": discount,
        "status": status,
        "sortCode": sortCode,
        "recommendNum": recommendNum,
        "updatedAt": updatedAt,
        "createdAt": createdAt,
      };
}

class AwardList {
  int? num;
  int? type;
  String? name;

  AwardList({this.num, this.type});

  AwardList.fromJson(Map<String, dynamic> json) {
    num = json['num'];
    type = json['type'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['num'] = num;
    data['type'] = type;
    data['name'] = name;
    return data;
  }
}

class DefaultAcgRanking {
  List<AllMediaInfo>? list;
  int? type;
  List<AwardList>? tab;

  DefaultAcgRanking({this.list, this.type});

  DefaultAcgRanking.fromJson(Map<String, dynamic> json) {
    List<AllMediaInfo> value = [];
    List? values = json['list'] as List?;
    value.addAll(values?.map((e) => AllMediaInfo.fromJson(e)).toList() ?? []);
    list = value;
    type = json['type'];

    if (json['tab'] != null) {
      tab = <AwardList>[];
      json['tab'].forEach((v) {
        tab!.add(new AwardList.fromJson(v));
      });
    }
  }
}

//领取奖励model
class ReceiveAwardModel {
  List<AwardList>? awardList;

  ReceiveAwardModel({this.awardList});

  ReceiveAwardModel.fromJson(Map<String, dynamic> json) {
    if (json['awardList'] != null) {
      awardList = <AwardList>[];
      json['awardList'].forEach((v) {
        awardList!.add(new AwardList.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.awardList != null) {
      data['awardList'] = this.awardList!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

//猜你喜欢
class PreferencesModel {
  int? defaultSortType;
  bool? hasNext;
  List<CartoonMediaInfo>? list;
  int? pageNumber;
  int? pageSize;
  String? sectionCover;
  String? sectionID;
  String? sectionName;
  List<SortType>? sortType;

  PreferencesModel({
    this.defaultSortType,
    this.hasNext,
    this.list,
    this.pageNumber,
    this.pageSize,
    this.sectionCover,
    this.sectionID,
    this.sectionName,
    this.sortType,
  });

  PreferencesModel.fromJson(Map<String, dynamic> json) {
    defaultSortType = json['defaultSortType'];
    hasNext = json['hasNext'];
    if (json['list'] != null) {
      list = [];
      json['list'].forEach((v) {
        list?.add(CartoonMediaInfo.fromJson(v));
      });
    }

    pageNumber = json['pageNumber'];
    pageSize = json['pageSize'];
    sectionCover = json['sectionCover'];
    sectionID = json['sectionID'];
    sectionName = json['sectionName'];

    if (json['sortType'] != null) {
      sortType = [];
      json['sortType'].forEach((v) {
        sortType?.add(SortType.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['defaultSortType'] = this.defaultSortType;
    data['hasNext'] = this.hasNext;
    if (this.list != null) {
      // data['list'] = this.list!.toJson();
    }
    data['pageNumber'] = this.pageNumber;
    data['pageSize'] = this.pageSize;
    data['sectionCover'] = this.sectionCover;
    data['sectionID'] = this.sectionID;
    data['sectionName'] = this.sectionName;
    if (this.sortType != null) {
      // data['sortType'] = this.sortType!.toJson();
    }
    return data;
  }
}

class SortType {
  String? key;
  int? value;

  SortType({this.key, this.value});

  SortType.fromJson(Map<String, dynamic> json) {
    key = json['key'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['key'] = this.key;
    data['value'] = this.value;
    return data;
  }
}

class CommuintyHotRecommend {
  List<CartoonMediaInfo>? novels;
  List<VideoModel>? pics;

  CommuintyHotRecommend.fromJson(Map<String, dynamic> json) {
    novels = (json['novel'] as List?)?.map((e) => CartoonMediaInfo.fromJson(e)).toList();
    pics = (json['pic'] as List?)?.map((e) => VideoModel.fromJson(e)).toList();
  }
}
